package com.em.widget.view;

import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.opengl.Visibility;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.em.baselibrary.R;
import com.em.view.util.ViewUtil;

import static com.em.baselibrary.R.id.img_title_memu_item_name;

public class SelectPopUpWindow extends PopupWindow implements OnClickListener {
	private View mMenuView;

	Activity context = null;
	String filename = null;
	public onPopViewListener popListener;
	public onDeleteViewListener deleteListener;

	@SuppressLint("ResourceAsColor")
	public SelectPopUpWindow(final Activity context, List<Map<String, Object>> list) {
		super(context);
		this.context = context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.layout_pop_view, null);
		LinearLayout ll_menu = new LinearLayout(context);
		ll_menu.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		ll_menu.setOrientation(LinearLayout.VERTICAL);
		ll_menu.removeAllViews();
		View view_menu_item = null;

		for (int i = 0; i < list.size(); i++) {
			view_menu_item = LayoutInflater.from(context).inflate(
					R.layout.item_title_menu, null);
			view_menu_item.setLayoutParams(new AbsListView.LayoutParams(ViewUtil.getActualSizeWithWidth(context, 350),
					ViewUtil.getActualSizeWithHeight(context, 96)));
			view_menu_item.setPadding(
					ViewUtil.getActualSizeWithWidth(context, 30),
					ViewUtil.getActualSizeWithWidth(context, 18),
					AbsListView.LayoutParams.WRAP_CONTENT,
					ViewUtil.getActualSizeWithWidth(context, 18));
			if(list.get(i).get("icon") != null && list.get(i).get("icon") instanceof Integer){
				((ImageView) view_menu_item.findViewById(R.id.img_title_memu_item_icon)).setImageResource((Integer)list.get(i).get("icon"));
			}else {
				((ImageView) view_menu_item.findViewById(R.id.img_title_memu_item_icon)).setVisibility(View.GONE);
			}
			if(list.get(i).get("name") != null && list.get(i).get("name") instanceof String){
				((TextView) view_menu_item.findViewById(img_title_memu_item_name)).setText(list.get(i).get("name").toString());
			}
			if (list.get(i).get("isEdit")!=null && list.get(i).get("isEdit").equals("Y")){
				ImageView deleteBtn = (ImageView) view_menu_item.findViewById(R.id.img_delete_memu_item_icon);
				deleteBtn.setVisibility(View.VISIBLE);
				deleteBtn.setOnClickListener(this);
				Object tag = list.get(i).get("tag");
				if (tag!=null){
					deleteBtn.setTag(tag);
				}else if (list.get(i).get("name")!=null){
					deleteBtn.setTag(list.get(i).get("name"));
				}
			}

			view_menu_item.setOnClickListener(this);
			ll_menu.addView(view_menu_item);
			if(i < list.size() - 1){
				ImageView v = new ImageView(context);
				LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1);
				v.setLayoutParams(lp1);
				v.setBackgroundColor(android.R.color.black);
				ll_menu.addView(v);
			}
		}

		// 设置按钮监听
		// 设置SelectPicPopupWindow的View
		this.setContentView(ll_menu);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(ViewUtil.getActualSizeWithWidth(context, 350));
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.mystyle);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.pop_layout2).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.item_title_menu_layout){
			TextView tv = (TextView) v.findViewById(R.id.img_title_memu_item_name);
			if(tv != null && popListener!=null){
				popListener.onViewSelectListener(tv.getText().toString());
			}
		}else if(v.getId() == R.id.img_delete_memu_item_icon){
			if ( deleteListener!=null ){
				deleteListener.onDelete(v);
			}
		}


	}
	
	public interface onPopViewListener{
		public void onViewSelectListener(String titleTxt);
	}


	public interface  onDeleteViewListener{
		public void onDelete(View v);
	}
	/**
	 * 在指定的view下面显示
	 * @param v
	 */
	public void showDropDown(View v){
		this.showAsDropDown(v,ViewUtil.getActualSizeWithWidth(context, 345), 0);
	}
}
